package mis.ds.ps.ui.swing;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ImportPreferencias extends JInternalFrame implements ActionListener {

	private static final long serialVersionUID = 4007211501165635576L;

	// Adapter for swing
	private ISwingAdapter swingAdapter;

	private final static String STEP1 = "Step1";
	private final static String STEP2 = "Step2";

	// buttons
	private JButton btnBack;
	private JButton btnNext;
	private JButton btnCancel;

	// panels
	private Step1Module2 panelStep1;
	private Step2Module2 panelStep2;
	private JPanel panelCards;
	
	// card layouts
	private CardLayout cardLayout;

	private boolean nextPossible = false;

	public ImportPreferencias() {
		super("Preferências Docentes...", 
	              true, //resizable
	              true, //closable
	              true, //maximizable
	              true);//iconifiable		

				// Create a new session
		swingAdapter = new SwingAdapter();
		swingAdapter.createNewSession("unnamed");

		// pack();
		setSize(800, 600);
		initComponents();

		cardLayout = (CardLayout) (panelCards.getLayout());
		cardLayout.show(panelCards, STEP1);

	}

	private void initComponents() {

		Container panelContent = this.getContentPane();
		
		panelStep1 = new Step1Module2(swingAdapter);
		panelStep2 = new Step2Module2(swingAdapter);

		JPanel panelFooter = new JPanel();
		Box boxFooter = new Box(BoxLayout.X_AXIS);

		// Create the panel that contains the "cards".
		panelCards = new JPanel(new CardLayout());
		panelCards.add(panelStep1, STEP1);
		panelCards.add(panelStep2, STEP2);

		btnBack = new JButton("< Back");
		btnNext = new JButton("Next >");
		btnCancel = new JButton("Cancel");

		btnBack.setEnabled(false);

		btnBack.addActionListener(this);
		btnNext.addActionListener(this);
		btnCancel.addActionListener(this);

		panelFooter.setLayout(new BorderLayout());
		panelFooter.add(new JSeparator(), BorderLayout.NORTH);

		boxFooter.setBorder(new EmptyBorder(new Insets(5, 10, 5, 10)));
		boxFooter.add(btnBack);
		boxFooter.add(Box.createHorizontalStrut(10));
		boxFooter.add(btnNext);
		boxFooter.add(Box.createHorizontalStrut(30));
		boxFooter.add(btnCancel);

		panelFooter.add(boxFooter, BorderLayout.EAST);
		panelContent.add(panelFooter, BorderLayout.SOUTH);
		panelContent.add(panelCards, BorderLayout.CENTER);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnNext) {
			System.out.println(panelStep1.getLstDocentes().getSelectedValue());
			
			if (panelStep1.getTxtFile().getText().equalsIgnoreCase("")) {
				JOptionPane.showMessageDialog(this, "Please choose file to import", "Inexistent File", JOptionPane.WARNING_MESSAGE);
			} else if (panelStep1.getLstDocentes().getSelectedValue() == null) {
				JOptionPane.showMessageDialog(this, "Please choose Docente", "Docente", JOptionPane.WARNING_MESSAGE);
			} else {

				System.out.println("next");
//				panelStep2.setResult(swingAdapter.simulateModule2());
				swingAdapter.simulateModule2();
				panelStep2.setResult();

				cardLayout.next(panelCards);
				btnBack.setEnabled(true);
				btnNext.setEnabled(false);
			}
		} else if (e.getSource() == btnBack) {
			System.out.println("back");
			cardLayout.previous(panelCards);
			btnNext.setEnabled(true);
			btnBack.setEnabled(false);
		} else if (e.getSource() == btnCancel) {
			this.setVisible(false);
		}
	}

	public void setNextPossible(boolean nextPossible) {
		this.nextPossible = nextPossible;
	}

	public boolean isNextPossible() {
		return nextPossible;
	}

}
